using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
using System.Drawing.Printing;
using System.IO;

namespace Skywave.Windows.Forms
{
    public partial class AdvancedPageSetupDialogForm : Form
    {
        public AdvancedPageSetupDialogForm()
        {
            InitializeComponent();
        }
        private IFormatter formatter = new BinaryFormatter();
        private bool PC = false;

        private PrintDocument mPrintDocument;
        public PrintDocument Document
        {
            get { return mPrintDocument; }
            set
            {
                mPrintDocument = value;
                if ((value == null)) return;
                RefreshByPrintDocument();
            }
        }

        private Skywave.ComponentModel.ProfilesHolder mProfilesHolder;
        [Browsable(false)]
        public Skywave.ComponentModel.ProfilesHolder ProfilesHolder
        {
            get { return mProfilesHolder; }
            set
            {
                mProfilesHolder = value;
                pmPageSettings.InitializeMe(mProfilesHolder);
            }
        }

        //[DefaultValue("")]
        //public string ProfilesPath
        //{
        //    get { return pmPageSettings.ProfilesPath; }
        //    set { pmPageSettings.ProfilesPath = value; }
        //}

        private void RefreshByPrintDocument()
        {
            try
            {
                PaperSize pkSize;
                PaperSource ps;
                int i;
                int int1;
                string str1;
                PrinterSettings.PaperSizeCollection xPaperSizes;
                PrinterSettings.PaperSourceCollection xPaperSources;
                PageSettings xDefaultPageSettings;
                if (mPrintDocument == null)
                {
                    this.Enabled = false;
                    return;
                }

                this.Enabled = true;
                if (PrinterSettings.InstalledPrinters.Count != 0)
                {
                    //Refresh Printers combobox
                    cmbPrinters.BeginUpdate();
                    cmbPrinters.Items.Clear();
                    int1 = -1;
                    for (i = 0; i < PrinterSettings.InstalledPrinters.Count; i++)
                    {
                        str1 = PrinterSettings.InstalledPrinters[i];
                        cmbPrinters.Items.Add(str1);
                        if (mPrintDocument.DefaultPageSettings.PrinterSettings.PrinterName == str1) int1 = i;
                    }
                    PC = true;
                    if (int1 >= 0) cmbPrinters.SelectedIndex = int1;
                    PC = false;
                    cmbPrinters.EndUpdate();

                    xDefaultPageSettings = mPrintDocument.DefaultPageSettings;
                    tpPage_cmbPageSize.DisplayMember = "PaperName";
                    tpPage_cmbPageSize.BeginUpdate();
                    tpPage_cmbPageSize.Items.Clear();
                    int1 = -1;
                    xPaperSizes = xDefaultPageSettings.PrinterSettings.PaperSizes;
                    str1 = xDefaultPageSettings.PaperSize.PaperName;
                    for (i = 0; i < xPaperSizes.Count; i++)
                    {
                        pkSize = xPaperSizes[i];
                        if (pkSize.PaperName == str1) int1 = i;
                        tpPage_cmbPageSize.Items.Add(pkSize);
                    }
                    PaperSize pkCustomSize1 = new PaperSize("Custom Paper Size", 100, 200);
                    tpPage_cmbPageSize.Items.Add(pkCustomSize1);
                    tpPage_cmbPageSize.EndUpdate();
                    if (int1 < 0)
                    {
                        pkCustomSize1.Width = xDefaultPageSettings.PaperSize.Width;
                        pkCustomSize1.Height = xDefaultPageSettings.PaperSize.Height;
                        tpPage_cmbPageSize.SelectedIndex = tpPage_cmbPageSize.Items.Count - 1;
                    }
                    else
                    {
                        tpPage_cmbPageSize.SelectedIndex = int1;
                    }
                    tpPage_lstPaperSource.DisplayMember = "SourceName";
                    tpPage_lstPaperSource.BeginUpdate();
                    tpPage_lstPaperSource.Items.Clear();
                    int1 = -1;
                    xPaperSources = xDefaultPageSettings.PrinterSettings.PaperSources;
                    str1 = xDefaultPageSettings.PaperSource.SourceName;
                    for (i = 0; i < xPaperSources.Count; i++)
                    {
                        ps = xPaperSources[i];
                        if (ps.SourceName == str1) int1 = i;
                        tpPage_lstPaperSource.Items.Add(ps);
                    }
                    tpPage_lstPaperSource.EndUpdate();
                    tpPage_lstPaperSource.SelectedIndex = int1;
                    PC = true;
                    tpMargins_txtLeft.Text = xDefaultPageSettings.Margins.Left.ToString();
                    tpMargins_txtRight.Text = xDefaultPageSettings.Margins.Right.ToString();
                    tpMargins_txtTop.Text = xDefaultPageSettings.Margins.Top.ToString();
                    tpMargins_txtBottom.Text = xDefaultPageSettings.Margins.Bottom.ToString();
                    if (xDefaultPageSettings.Landscape)
                    {
                        tpMargins_rbHorizontal.Checked = true;
                    }
                    else
                    {
                        tpMargins_rbVertical.Checked = true;
                    }
                    PC = false;
                }
            }
            catch (Exception)
            {
            }
        }

        private void cmdOK_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            Close();
        }

        private void cmdCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            Close();
        }

        private void tpPage_cmbPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            PaperSize xPaperSize = (PaperSize)tpPage_cmbPageSize.SelectedItem;
            mPrintDocument.DefaultPageSettings.PaperSize = xPaperSize;
            PC = true;
            tpPage_txtPageWidth.Text = xPaperSize.Width.ToString();
            tpPage_txtPageHeight.Text = xPaperSize.Height.ToString();
            PC = false;
        }

        private int MaInt(string str)
        {
            return (str == "") ? 0 : int.Parse(str);
        }
        private void tpPage_txtPageHeightOrWidth_TextChanged(object sender, EventArgs e)
        {
            if (PC) return;
            if (tpPage_cmbPageSize.Items.Count < 1) return;
            PaperSize xPaperSize = (PaperSize)tpPage_cmbPageSize.Items[tpPage_cmbPageSize.Items.Count - 1];
            xPaperSize.Height = MaInt(tpPage_txtPageHeight.Text);
            xPaperSize.Width = MaInt(tpPage_txtPageWidth.Text);
            tpPage_cmbPageSize.SelectedItem = xPaperSize;
        }

        private void tpPage_lstPaperSource_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (tpPage_lstPaperSource.SelectedIndex < 0) return;
            mPrintDocument.DefaultPageSettings.PaperSource = (PaperSource)tpPage_lstPaperSource.SelectedItem;
        }

        private void tpMargins_rbVertical_CheckedChanged(object sender, EventArgs e)
        {
            if (mPrintDocument == null || PC) return;
            mPrintDocument.DefaultPageSettings.Landscape = !tpMargins_rbVertical.Checked;
        }

        private void cmbPrinters_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (PC) return;
            if (cmbPrinters.SelectedIndex < 0) return;
            mPrintDocument.DefaultPageSettings.PrinterSettings.PrinterName = cmbPrinters.SelectedItem.ToString();
            RefreshByPrintDocument();
        }

        private void tpMargins_txtTop_TextChanged(object sender, EventArgs e)
        {
            if (mPrintDocument == null || PC) return;
            mPrintDocument.DefaultPageSettings.Margins.Top = MaInt(tpMargins_txtTop.Text);
        }

        private void tpMargins_txtBottom_TextChanged(object sender, EventArgs e)
        {
            if (mPrintDocument == null || PC) return;
            mPrintDocument.DefaultPageSettings.Margins.Bottom = MaInt(tpMargins_txtBottom.Text);
        }

        private void tpMargins_txtLeft_TextChanged(object sender, EventArgs e)
        {
            if (mPrintDocument == null || PC) return;
            mPrintDocument.DefaultPageSettings.Margins.Left = MaInt(tpMargins_txtLeft.Text);

        }

        private void tpMargins_txtRight_TextChanged(object sender, EventArgs e)
        {
            if (mPrintDocument == null || PC) return;
            mPrintDocument.DefaultPageSettings.Margins.Right = MaInt(tpMargins_txtRight.Text);
        }

        private void pmPageSettings_Serialize(object sender, Skywave.Windows.Forms.ProfilesManagerProfileEventArgs e)
        {
            Stream fs = e.Content;
            PageSettings xDefaultPageSettings = mPrintDocument.DefaultPageSettings;

            formatter.Serialize(fs, xDefaultPageSettings.PrinterSettings.PrinterName);
            formatter.Serialize(fs, xDefaultPageSettings.PaperSize.Width);
            formatter.Serialize(fs, xDefaultPageSettings.PaperSize.Height);
            formatter.Serialize(fs, xDefaultPageSettings.PaperSize.PaperName);
            formatter.Serialize(fs, xDefaultPageSettings.PaperSource.SourceName);
            formatter.Serialize(fs, xDefaultPageSettings.Margins.Left);
            formatter.Serialize(fs, xDefaultPageSettings.Margins.Right);
            formatter.Serialize(fs, xDefaultPageSettings.Margins.Top);
            formatter.Serialize(fs, xDefaultPageSettings.Margins.Bottom);
            formatter.Serialize(fs, xDefaultPageSettings.Landscape);
        }

        private void pmPageSettings_Deserialize(object sender, Skywave.Windows.Forms.ProfilesManagerProfileEventArgs e)
        {
            Stream fs = e.Content;

            string xPrinterName = (string)formatter.Deserialize(fs);
            PageSettings xDefaultPageSettings = mPrintDocument.DefaultPageSettings;
            foreach (string xStr in PrinterSettings.InstalledPrinters)
            {
                if (xPrinterName == xStr)
                {
                    xDefaultPageSettings.PrinterSettings.PrinterName = xPrinterName;
                    int xWidth = (int)formatter.Deserialize(fs);
                    int xHeight = (int)formatter.Deserialize(fs);
                    string xPaperName = (string)formatter.Deserialize(fs);
                    PaperSize xPaperSize = new PaperSize(xPaperName, xWidth, xHeight);
                    xDefaultPageSettings.PaperSize = xPaperSize;
                    string xSourceName = (string)formatter.Deserialize(fs);
                    foreach (PaperSource xPaperSource in xDefaultPageSettings.PrinterSettings.PaperSources)
                    {
                        if (xPaperSource.SourceName == xSourceName)
                        {
                            xDefaultPageSettings.PaperSource = xPaperSource;
                            break;
                        }
                    }
                    xDefaultPageSettings.Margins.Left = (int)formatter.Deserialize(fs);
                    xDefaultPageSettings.Margins.Right = (int)formatter.Deserialize(fs);
                    xDefaultPageSettings.Margins.Top = (int)formatter.Deserialize(fs);
                    xDefaultPageSettings.Margins.Bottom = (int)formatter.Deserialize(fs);
                    xDefaultPageSettings.Landscape = (bool)formatter.Deserialize(fs);
                    break;
                }
            }
            RefreshByPrintDocument();
        }
    }
}